﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace OpenClientX.Utils
{
    class HoneywellPrintHelper
    {
        [DllImport("HoneywellPrint.dll")]
        static extern int PRN_ConnectUsbPort(string portName, int readTimeOut);

        [DllImport("HoneywellPrint.dll")]
        static extern int PRN_OpenPrinter(string portName);

        [DllImport("HoneywellPrint.dll")]
        static extern int PRN_ClosePrinter();
        [DllImport("HoneywellPrint.dll")]
        static extern int PRN_SendBuffer(string data);

        [DllImport("HoneywellPrint.dll")]
        static extern int PRN_AddImageToLabelEx(string imgFilePath, int posX, int posY, int dir, int align);

        [DllImport("HoneywellPrint.dll")]
        static extern int PRN_AddBarcodeToLabel(string codeToPrint, string codeType, int height, int posX, int posY, int dir, int align, int enlargeWidth);

        [DllImport("HoneywellPrint.dll")]
        static extern int PRN_SetMediaWidth(int width);

        [DllImport("HoneywellPrint.dll")]
        static extern int PRN_SetMediaLength(int Length);

        [DllImport("HoneywellPrint.dll")]
        static extern int PRN_WriteConfig();

        [DllImport("HoneywellPrint.dll")]
        static extern int PRN_SetMediaType(int mtype);

        [DllImport("HoneywellPrint.dll")]
        static extern int PRN_SetMediaMarginX(int Margin);

        [DllImport("HoneywellPrint.dll")]
        static extern int PRN_PrintLabel(int PrintCount);


        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="path">条形码路径</param>
        /// <param name="printerName">调用的打印机名称/param>
        public static void StartPrint(String path,String printerName)
        {
            //Test for send cmd buffer
            if (PRN_OpenPrinter(printerName) == 0)
            {
                //String prnCmd = "nasc 1\n nascd \"\"\n PRPOS 50,10\n FONT\"Swiss 721 BT\",10\n PT \"Honey Test\"\n PF\n";
                //String prnCmd = "PP 150.150\n " +
                //    //"PT \"Honeywell-PC42d\" \n " +
                //    "BARSET \"CODE39\",3,1,2,100" +
                //    "ALIGN 2" +
                //    "BARFONT \"Univers\",10,8,5,1,1,100 ON \n" +
                //    "BF ON \n" +
                //    "PB\"TYA25540000000818110080093TY1\" \n" +
                //    "PF\"";
                //PRN_SendBuffer(prnCmd);
                int ret = 0;
                PRN_SetMediaType(1);
                ret = PRN_AddImageToLabelEx(path, 50, 20, 1, 1);
                ret = PRN_PrintLabel(1);
            }
            PRN_ClosePrinter();
        }

    }
}
